package com.wolf.controller;

import com.wolf.pojo.Wl_banner;
import com.wolf.pojo.Wl_subject;
import com.wolf.service.Wl_bannerService;
import com.wolf.util.Math;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.nio.channels.Channel;
import java.util.List;

@Controller
@ComponentScan
public class Wl_bannerController {

    @Autowired
    public Wl_bannerService wl_bannerService;

    public void setWl_bannerService(Wl_bannerService wl_bannerService) {
        this.wl_bannerService = wl_bannerService;
    }

    /**
     * 查询所有
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_banner_getall")
    public String getAll(ModelMap modelMap){

        List<Wl_banner> list = wl_bannerService.getAll();

        for(Wl_banner wl:list){
            String channels = "";
            String channel = wl.getChannel();
            for(int i = 0;i<channel.length();i++){
                if(channel.substring(i,i+1).equals("1")){
                    channels = channels+"微信  ";
                }else if(channel.substring(i,i+1).equals("2")){
                    channels = channels+"pc  ";
                }else if(channel.substring(i,i+1).equals("3")){
                    channels = channels+"ios  ";
                }else if(channel.substring(i,i+1).equals("4")){
                    channels = channels+"android  ";
                }
            }
            wl.setChannel(channels);
        }

        modelMap.addAttribute("wl_banner_list",list);

        return "table_wl_banner";
    }

    /**
     * banner修改
     * @return
     */
    @RequestMapping("wl_banner_update")
    @ResponseBody
    public int wl_banner_update(String[] image,String sid, String bannername, String id, String channel, String url, int status, Integer index){

        Wl_subject wl_subject = new Wl_subject();
        wl_subject.setId(sid);
        wl_subject.setUrl(url);

        //创建对象并赋值
        Wl_banner wl = new Wl_banner();

        //图片
        if(image!=null){
            String images="";
            for(int i=0;i<image.length;i++){
                //img是我自己在项目中添加的一个文件夹
                images+="img/"+image[i]+",/";
            }
            images=images.substring(0,images.lastIndexOf(",/"));
            wl.setBannerimage(images);
        }

        wl.setId(id);
        wl.setBannername(bannername);
        wl.setChannel(channel);
        wl.setWl_subject(wl_subject);
        wl.setStatus((byte)status);
        wl.setIndex(index);

        return wl_bannerService.updateByPrimaryKeySelective(wl);
    }
    /**
     * banner删除
     * @return
     */
    @RequestMapping("wl_banner_delete")
    public String wl_banner_delete(String id){

        int i = wl_bannerService.deleteByPrimaryKey(id);

        return "redirect:wl_banner_getall";
    }

    /**
     * 查询单个对象
     * @return
     */
    @RequestMapping("wl_banner_byid")
    public String wl_banner_byid(String id,ModelMap modelMap){

        Wl_banner wl = wl_bannerService.selectByPrimaryKey(id);

        modelMap.addAttribute("wl_banner_bean",wl);

        return "wl_banner_update";
    }

    /**
     * 修改对象的状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("wl_banner_updatestatus")
    public String wl_banner_updatestatus(String id,Integer status){
        Wl_banner wl = new Wl_banner();
        if(status==1)
            wl.setStatus((byte)0);
        else
            wl.setStatus((byte)1);


        wl.setId(id);
        wl.setWl_subject(new Wl_subject());
        wl_bannerService.updateByPrimaryKeySelective(wl);

        return "redirect:wl_banner_getall";
    }

    /**
     * banner新增
     * @param bannername
     * @param channel
     * @param url
     * @param status
     * @param index
     * @return
     */
    @RequestMapping("wl_banner_insert")
    @ResponseBody
    public int wl_banner_insert(String[] image,String bannername,String channel,String url,int status,Integer index){

        //创建对象并赋值
        Wl_banner wl = new Wl_banner();

        //图片
        if(image!=null){
            String images="";
            for(int i=0;i<image.length;i++){
                //img是我自己在项目中添加的一个文件夹
                images+="img/"+image[i]+",/";
            }
            images=images.substring(0,images.lastIndexOf(",/"));
            wl.setBannerimage(images);
        }

        wl.setId(Math.random());
        wl.setBannername(bannername);
        wl.setChannel(channel);
        wl.setSubjectid(url);
        wl.setStatus((byte)status);
        wl.setIndex(index);

        return wl_bannerService.insertSelective(wl);
    }

    /**
     * 模糊查询
     * @param modelMap
     * @param channel
     * @param status
     * @param bannername
     * @param starttime
     * @param endtime
     * @return
     */
    @RequestMapping("wl_banner_getselective")
    public String getSelective(ModelMap modelMap,String channel,String status,String bannername,String starttime,String endtime){

        Integer channels = null;
        Integer statuss = null;

        if(channel!="" && channel!=null){

            channels = Integer.parseInt(channel);
        }

        if(status!=""  && status!=null){
            statuss = Integer.parseInt(status);
        }

        List<Wl_banner> list = wl_bannerService.getSelective(channels,statuss,bannername,starttime,endtime);

        for(Wl_banner wl:list){
            String channels1 = "";
            String channel1 = wl.getChannel();
            for(int i = 0;i<channel1.length();i++){
                if(channel1.substring(i,i+1).equals("1")){
                    channels1 = channels1+"微信  ";
                }else if(channel1.substring(i,i+1).equals("2")){
                    channels1 = channels1+"pc  ";
                }else if(channel1.substring(i,i+1).equals("3")){
                    channels1 = channels1+"ios  ";
                }else if(channel1.substring(i,i+1).equals("4")){
                    channels1 = channels1+"android  ";
                }
            }
            wl.setChannel(channels1);
        }

        modelMap.addAttribute("wl_banner_list",list);

        return "table_wl_banner";
    }
}
